package com.villaday.idf.custom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.util.ToastUtil;


/**
 * 填写邀请码对话框
 */
@SuppressLint("ValidFragment")
public class InvitationCardDialog extends BaseDialog implements View.OnClickListener {

    EditText etInvitateCode;
    Context context;
    private boolean isUpdating;//图片是否正在上传
    private LoadingDialog loadingDialog;
    String Code;
    private InvitateCardListener icListener;


    public static InvitationCardDialog newInstance(Context context) {
        InvitationCardDialog dialog = new InvitationCardDialog(context);
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    public InvitationCardDialog(Context context) {
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_invitation_card, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(true);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateTop);

        etInvitateCode = (EditText) view.findViewById(R.id.et_input_code);
        TextView btnConfirm = (TextView) view.findViewById(R.id.btn_confirm);
        TextView btnCode = (TextView) view.findViewById(R.id.btn_card);
        ImageView ivClose = (ImageView) view.findViewById(R.id.iv_close_dialog);


        btnConfirm.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        btnCode.setOnClickListener(this);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    public void setListener(InvitateCardListener icListener) {
        this.icListener = icListener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            Code = etInvitateCode.getText().toString();
            if (TextUtils.isEmpty(Code)) {
                ToastUtil.initToast(getActivity(), "请填写邀请码");
            } else {
                icListener.finishInvitateCard(Code, true);
                dismiss();
            }
        } else if (v.getId() == R.id.btn_card) {
            icListener.finishInvitateCard(Code, false);
            dismiss();
        } else if (v.getId() == R.id.iv_close_dialog) {
            dismiss();
        }
    }

    public interface InvitateCardListener {
        void finishInvitateCard(String invateCode, boolean isInput);
    }


}
